package com.gs.wzmanager.controller;

import com.gs.wzmanager.entity.Resource;
import com.gs.wzmanager.entity.Role;
import com.gs.wzmanager.entity.User;
import com.gs.wzmanager.service.RoleService;
import com.gs.wzmanager.service.UserService;
import com.gs.wzmanager.utils.HttpUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@Controller
@RequestMapping("/user")
public class UserController {

        @Autowired
        private UserService userService;
        @Autowired
        private RoleService roleService;

        private Logger logger = LoggerFactory.getLogger(UserController.class);

        //无权限
        @RequestMapping("/noAuthority")
        public String noAuthority() {
                return "noAuthority";
        }


        @RequestMapping("/addView")
        @ResponseBody
        public List addView() {
                List<Role> roles = roleService.queryAllRole();
                return roles;
        }

        //添加新用户
        @RequestMapping(value = "/addUser" ,method = RequestMethod.POST)
        public ResponseEntity addUser(@RequestBody User user) {
                user.setId(UUID.randomUUID().toString());
                userService.addUser(user);
                //logger.info("插入的userId为->{}",user.getId());
                List<String> roles = user.getRolesitems();
                logger.info("user->{}",user);
                if (roles.size()!=0) {
                    for (String str : roles) {
                        int roleId = Integer.parseInt(str);
                        userService.addUserRole(user.getId(), roleId);
                    }
                }
                return HttpUtils.buildStringResponse("OK", HttpStatus.OK);
        }

        @RequestMapping("/listView")
        public String turnToView(){
                return "user/list";
        }

        //用户列表
        @RequestMapping("/list")
        @ResponseBody
        public List<User> list() {
                List<User> users = userService.queryAllUser();
                return users;
        }

        //用户权限资源
        @RequestMapping("/res/{id}")
        @ResponseBody
        public Map res(@PathVariable String id) {
                Map map = new HashMap();
                User user = userService.queryUserById(id);
                List<Resource> resources = userService.queryResourceByUserId(id);
                map.put("userInfo",user);
                map.put("resources",resources);
                return map;
        }

        //更新返回界面
        @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
        @ResponseBody
        public Map update(@PathVariable String id) {
                User user = userService.queryUserById(id);
                List<Role> roles = roleService.queryAllRole();
                List<Role> my_roles = userService.queryRoleByUserId(id);
                List<Integer> rids = new ArrayList<Integer>();
                for (Role role : my_roles) {
                        rids.add(role.getId());
                }
                Map map = new HashMap();
                map.put("userInfo",user);
                map.put("roleList",roles);
                map.put("myRole",rids);
                return map;
        }

        //更新用户信息
        @RequestMapping(value = "/update", method = RequestMethod.POST)
        public ResponseEntity update(@RequestBody User user) {
                logger.info("user->{}",user);
                List<String> roles = user.getRolesitems();
                userService.updateSimpleUser(user);
                if (roles.size()!=0) {
                        //删除所有该用户的角色记录，重新填写角色记录
                        userService.deleteUserRoleByUserId(user.getId());
                        for (String str : roles) {
                                int roleId = Integer.parseInt(str);
                                userService.addUserRole(user.getId(), roleId);
                        }
                }
                return HttpUtils.buildStringResponse("OK",HttpStatus.OK);
        }

        //删除用户
        @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
        public ResponseEntity del(@PathVariable String id) {
                userService.deleteUser(id);
                return HttpUtils.buildStringResponse("OK",HttpStatus.OK);
        }


        @RequestMapping("/infoView")
        public String infoView(){
                return "info/list";
        }

        //查看用户信息
        @RequestMapping("/info")
        @ResponseBody
        public Map info() {
                Subject subject = SecurityUtils.getSubject();
                String phone = subject.getPrincipal().toString();
                User user = userService.queryUserByPhone(phone);
                if (user == null) {
                        return null;
                }
                String id = user.getId();
                List<Role> roles = userService.queryRoleByUserId(id);
                List<Resource> resources = userService.queryResourceByUserId(id);
                Map map = new HashMap();
                map.put("user",user);
                map.put("roleList",roles);
                map.put("resList",resources);
                return map;
        }

        @RequestMapping("/find")
        @ResponseBody
        public List<User> find(@RequestParam String name){
                logger.info("name",name);
               return  userService.queryUserByName(name);
        }

}
